package org.agile2soft.nexoma.tapestry.components;

import nu.localhost.tapestry5.springsecurity.services.LogoutService;

//import org.apache.tapestry5.MarkupWriter;
//import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Value;
import org.apache.tapestry5.services.Request;
//import org.springframework.security.Authentication;
//import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

public class UserLoginDetails {

    @Inject @Value("${spring-security.check.url}")
    private String checkUrl;

    private boolean failed;

    public boolean isFailed() {
        return failed;
    }

    @Inject
    private LogoutService logoutService;

    @OnEvent(component="logout")
    public void doLogout(){
        logoutService.logout();
    }

    @Inject
    private Request request;

    public String getLoginCheckUrl() {
        return request.getContextPath() + checkUrl;
    }

    @Property
    private Authentication authentication;

    @SetupRender
    void setupRender () {
        authentication = SecurityContextHolder.getContext().getAuthentication();
    }

    public String getName() {
        return authentication.getName();
    }

    public String[] getRoles() {
        return new String [] {"ROLE_USER","ROLE_ADMIN"};
    }

}
